import { request, propsNotEmpty, memoizePromise } from './utils';

async function isValidToken(token, options) {
  propsNotEmpty(options, ['API_BASE_URL']);

  const {
    API_BASE_URL, 
    HEADERS_CONFIG: headersConfig
  } = options;
  try {
    if (!token) throw new Error('token 不存在');

    const data = await request(`${API_BASE_URL}wechatMiniApp/checkToken`, {
      token
    }, 'GET', headersConfig);
    if (data) {
      console.log('token 校验通过', data);
      return data;
    } else {
      throw new Error('token失效')
    }
  } catch (error) {
    throw error
  }
}
let validToken = memoizePromise(isValidToken)
export default validToken;